//
//  main.m
//  OC04-解析HomeWork
//
//  Created by dllo on 16/7/7.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"
#import "Manager.h"
int main(int argc, const char * argv[]) {
    /**
     *  处理⽂件crayons.txt中的⽂本信息，⽂本内容是关于颜⾊的，每⾏都是⼀ 个颜⾊的信息，例如：Almond #EED9C4，前⼀个字符串是颜⾊的名称，后⼀ 个字符串是颜⾊的16进制⾊值，处理⽂本完成如下需求:
     使⽤字典管理所有的颜⾊，即字典中存储的是多个键值对，颜⾊名称为 key，16进制颜⾊值（不带#）是value。
     使⽤⼀个新的字典管理颜⾊，对颜⾊进⾏分类管理，即：“A”，“B”，“C”… 即这个字典包含多个键值对，key是26个字⺟，value是数组，数组⾥存放的是 Color对象（包含name和colorValue）。需要⾃⼰创建Color类。
     */
#if 0
    {
    NSString * dataString = [NSString stringWithContentsOfFile:@"/Users/dllo/DLS160710/Objective-C/HomeWork/OC04-解析HomeWork/OC04-解析HomeWork/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [dataString componentsSeparatedByString:@"\n"];
    NSMutableArray *arr1 = [NSMutableArray array];
    for (int i = 0; i < arr.count; i++) {
        NSArray  *arr2 = [arr[i] componentsSeparatedByString:@" #"];
        [arr1 addObjectsFromArray:arr2];
    }
    NSLog(@"%@", arr1);
    NSMutableArray *array1 = [NSMutableArray array];
    NSMutableArray *array2 = [NSMutableArray array];
    for (int i = 0; i < arr1.count; i++) {
        if ((i + 2) % 2 == 0) {
            [array1 addObject:arr1[i]];
        } else{
            [array2 addObject:arr1[i]];
        }
    }
    NSMutableDictionary *colorDic = [NSMutableDictionary dictionaryWithObjects:array2 forKeys:array1];
    Manager *colorDictionary = [[Manager alloc] init];
    NSMutableDictionary *dic = [colorDictionary colorDictionary];
    for (NSString *key in colorDic) {
        Color *color = [[Color alloc] initWithName:key colorValue:[colorDic objectForKey:key]];
        NSMutableArray *arrayM1 = [dic valueForKey:[color.name substringToIndex:1]];
        [arrayM1 addObject:color];
    }
    NSLog(@"%@",dic);
    }
#endif
    NSString *path = @"/Users/dllo/DLS160710/Objective-C/HomeWork/OC04-解析HomeWork/OC04-解析HomeWork/crayons.txt";
    // 通过文件路径读取文件中的字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", sourceString);
    // 根据\n 切割字符串
    NSArray *colorStringArray = [sourceString componentsSeparatedByString:@"\n"];
    NSLog(@"%@", colorStringArray);
    // 声明存储颜色的可变字典
    NSMutableDictionary *colorDictionary = [NSMutableDictionary dictionary];
    for (int i = 0; i < colorStringArray.count; i++) {
        if ([colorStringArray[i] length] > 0) {
            NSArray *color = [colorStringArray[i] componentsSeparatedByString:@" #"];
            [colorDictionary setObject:[color lastObject] forKey:[color firstObject]];
        }
    }
    NSLog(@"%@", colorDictionary);
    // 创建新字典
    NSMutableDictionary *newColorDic = [NSMutableDictionary dictionary];
    // 取到老字典中所有key值
    NSArray *allKeys = colorDictionary.allKeys;
    // 取首字母
    for (int i = 0; i < allKeys.count; i++) {
        NSString *subString = [allKeys[i] substringWithRange:NSMakeRange(0, 1)];
        // 通过首字母取出对应的Array
        NSMutableArray *colorArray = [newColorDic objectForKey:subString];
        // 根据老字典的key和value生成新的color对象
        Color *newColor = [[Color alloc] init];
        newColor.name = allKeys[i];
        newColor.colorValue = [colorDictionary objectForKey:allKeys[i]];
        // 判断从新字典中取出的数组是否为空
        // 为空说明还没有存过对应首字母的数组
        if (colorArray == nil) {
            // 创建
            colorArray = [NSMutableArray arrayWithObject:newColor];
            // 赋值给对应的首字母
            [newColorDic setObject:colorArray forKey:subString];
        } else {
            // 如果数组不为空, 直接添加
            [colorArray addObject:newColor];
        }
    }
    NSLog(@"%@", newColorDic);    return 0;
}
